package m.jclecai.com.view.common;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import m.jclecai.com.R;
import m.jclecai.com.base.recyclerview.BaseRecyclerViewAdapter;
import m.jclecai.com.base.recyclerview.BaseViewHolder;
import m.jclecai.com.bean.common.ShareBean;

import static m.jclecai.com.utils.PackageCompareUtil.getIconResourceByPackage;

/**
 * Created by andy on 2017/7/14.
 *  这里是用来实现分享的一个组合的自定义View;
 */
public class ShareView extends LinearLayout {

    private List<ShareBean> mShareList;
    private ShareAdapter mShareAdapter;
    private LinearLayout mLlBottom;
    private RecyclerView mRvShare;
    private Button mBtnCancel;

    public ShareView(Context context) {
        this(context,null);
    }

    public ShareView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ShareView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View.inflate(context, R.layout.layout_share_view,this);
        init();
    }

    private void init() {
        mLlBottom = ((LinearLayout) findViewById(R.id.ll_bottom));
        mRvShare = ((RecyclerView) findViewById(R.id.rv_share));
        mBtnCancel = ((Button) findViewById(R.id.btn_cancel));


        findViewById(R.id.view_blank).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //close
                closeShareAnimation();
            }
        });
        mBtnCancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //close
                closeShareAnimation();
            }
        });
        initShareView();
        initShareData();
    }

    /**
     * 这里是分享的view；
     */
    private void initShareView() {

        mLlBottom.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mLlBottom.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                mLlBottom.setTranslationY(mLlBottom.getHeight());
            }
        });


        mRvShare.setLayoutManager(new GridLayoutManager(getContext(),4));

        mShareAdapter = new ShareAdapter(null);
        mRvShare.setAdapter(mShareAdapter);
    }


    private void initShareData() {

        mShareList = new ArrayList<>();
        mShareList.add(new ShareBean(R.drawable.ic_tg_db_wx,"微信", Wechat.NAME));
        mShareList.add(new ShareBean(R.drawable.ic_tg_db_pyq,"朋友圈", WechatMoments.NAME));
        mShareAdapter.refresh(mShareList);
    }


    public class ShareAdapter extends BaseRecyclerViewAdapter<ShareBean> {

        public ShareAdapter(List<ShareBean> DList) {
            super(DList);
        }

        @Override
        public int getItemView() {
            return R.layout.item_share;
        }

        @Override
        protected void bindViewHolder(BaseViewHolder holder, final ShareBean bean, int position) {

            holder.getTextView(R.id.tv_share_text).setText(bean.getDrawableDesc());
            holder.getImageView(R.id.iv_share_icon).setImageResource(bean.getDrawableResId());
            //这里我们需要执行不同的逻辑
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //之类我们要进行不同的跳转；
                    closeShareAnimation();
                    share(bean.getShareType());
                }
            });
        }
    }



    public void share(String platformName) {

        Platform.ShareParams wechatMoments = new Platform.ShareParams();
        wechatMoments.setTitle("乐彩神彩票");
        wechatMoments.setText("快点来加入我们吧！！！");
        wechatMoments.setUrl("http://m.yuecp.net/");
       // wechatMoments.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");

        wechatMoments.setImageData(BitmapFactory.decodeResource(getContext().getResources(),getIconResourceByPackage()));
        wechatMoments.setShareType(Platform.SHARE_WEBPAGE);
        Platform weixin = ShareSDK.getPlatform(platformName);

        if(mListener!=null){
            weixin.setPlatformActionListener(mListener);
        }else{
            weixin.setPlatformActionListener(new PlatformActionListener() {
                @Override
                public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {

                    // DisPlay("分享成功");
                    Toast.makeText(getContext(),"分享成功",Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onError(Platform platform, int i, Throwable throwable) {

                    // DisPlay("分享失败");
                    Toast.makeText(getContext(),"分享失败",Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onCancel(Platform platform, int i) {

                    // DisPlay("分享取消");
                    Toast.makeText(getContext(),"分享取消",Toast.LENGTH_SHORT).show();
                }
            });
        }
        weixin.share(wechatMoments);
    }


    private void OpenShareAnimation() {
        ObjectAnimator.ofFloat(mLlBottom, "TranslationY", mLlBottom.getHeight(), 0).setDuration(200).start();
    }

    private void closeShareAnimation() {
        ObjectAnimator.ofFloat(mLlBottom, "TranslationY", 0, mLlBottom.getHeight()).setDuration(200).start();
    }

    public void share(){
        OpenShareAnimation();
    }

    public PlatformActionListener mListener;

    public PlatformActionListener getListener() {
        return mListener;
    }

    public void setShareListener(PlatformActionListener listener) {
        this.mListener = listener;
    }
}
